package com.firexxx.study.web.response;

import com.firexxx.study.exception.BusinessException;
import lombok.Data;
import lombok.ToString;

/**
 * @description: TODO
 * @throws:
 * @author: zhuangzhang2
 * @date: 2020/04/09 17:03
 */
@Data
@ToString
public final class RestResponse<T> {
    private String code;
    private String desc;
    private T data;

    public RestResponse(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public RestResponse(String code, String desc, T data) {
        this.code = code;
        this.desc = desc;
        this.data = data;
    }

    public static RestResponse success() {
        return new RestResponse(Success.SUCCESS.getCode(), Success.SUCCESS.getDesc());
    }

    public static <T> RestResponse success(T data) {
        return new RestResponse(Success.SUCCESS.getCode(), Success.SUCCESS.getDesc(), data);
    }

    public static RestResponse error(BusinessError error) {
        return new RestResponse(error.getCode(), error.getDesc());
    }

    public static RestResponse error(String code, String desc) {
        return new RestResponse(code, desc);
    }

    public static RestResponse error(BusinessException ex) {
        return new RestResponse(ex.getError().getCode(), ex.getFirstNonNullMessage());
    }
}